Extending Design Patterns into RIA
Table of Contents
Patterns in a Rich Internet Application Architecture
In the course of writing traditional web deployed applications, design patterns in enterprise development emerged to solve recurring problems. As much as the tier in a Rich Internet Application is an evolution from the tier in a traditional web model, so is the progression of the patterns used to build RIAs. Here, the term pattern refers to a generalized solution to a recurring design problem in software development.
The next section describes how you move functionality from the traditional front controller and business delegate patterns to new client patterns. As in any application design, you apply patterns when they suit your needs. The high-level patterns presented here address issues common to client/server communications; later articles will discuss these client patterns in more detail.
Two common problems in software development are how you communicate to your business logic and how you communicate from the client to the server. Establishing communication to business logic requires that you do so without coupling it so tightly to the view that a change (either in the logic or view) affects the entire application. When you communicate to the server, you need to ensure each request contains the necessary markup to establish security, session, or state management. You must establish each message to the server as a valid request or inspect the message for tokens used by the entire application.
There are a variety of languages that implement a traditional Internet model, the J2EE environment being prominent. In a Java environment, you often use a business delegate pattern to structure communication to business logic. This pattern, in its simplest form, uses a wrapper around the actual business logic. This wrapper translates an invocation request from the client to the proper request on the business logic component. To solve the server communication issue, many use the front controller pattern. This pattern routes all requests to a single controller file that validates security and establishes the correct flow of control so that it can create the correct response in the form of an HTML page. These two patterns often work together in a variety of composite structures.
Implementing a Rich Internet Application architecture is not terribly different from implementing a traditional web client, except that you move some of these activities from the server to the client. From an application design perspective, two high-level patterns emerge: service brokering and client component to server communication. Service brokering allows individual components to request an application service while abstracting the details of the service’s location or how it processes requests. The service broker provides the connectors for the individual component communication. In the client component to server communication, you provide connectivity from the component to the server instead of connecting at the page level. For each component, you provide the appropriate application interface logic, including asynchronous event handling. You can, of course, still create a centralized communication structure in your client to communicate to the server, depending on your application needs.
Service Broker
A primary goal for an application is to loosely couple the business logic and the interface logic so that changes in the application interface do not influence business logic, and the business logic requires no knowledge of how it is called. As noted, in a traditional environment, you might address this with a business delegate pattern. In a rich client you need to consider both the component and service requirements. The component requirements are as follows:
- Provide a standard interface to the remote service (standard methods for triggering methods, setting callbacks, and so forth).
- Abstract the remote service so it can be local or remote.
- Abstract how the call is made (SOAP, RMI, and so forth.
- Obfuscate when the call is made securely or anonymously.
- Provide handles to the components for incoming requests.
- Provide a location to connect to a service.
The service requirements include:
- Abstract the caller.
- Provide a standard way to trigger callback events.
One design that meets these requirements contains two types of classes, as shown Figure 4:
Figure 4. Design pattern for the service broker.
The first class, service broker, is responsible for creating the correct bridge adaptor to the remote service and maintaining a list of available services and their definitions.
The second class is a bridge component, a conduit between the user interface component and the service. The bridge component initializes based on the service definition and knows how to communicate to a local or remote service, knows how to communicate securely over different communication protocols, and knows which service to reference. The bridge contains the handle to the component so that it can provide direct callback methods to the component on success, failure, or request for more information from the service. The following is a diagram that shows this relationship:
Figure 5. The relationship between the classes
To understand this further, take a simple banking application for example. It includes variety of user options for users to manage their account and make inquires to the bank. The application is constructed in the tiered model noted in the previous section, where the client tier provides a rich interface that authenticates users, enables them to look at account balances and transactions, transfer money between accounts, or log a customer service request. The next section describes how you might construct the interface and focuses on how you set up communication from the client to the middleware tier.
The middleware tier contains the remote services. In the banking example, you might have a customerRequest service that resides on a different server since it ties into an existing Customer Relationship Management (CRM) system. The goal of a communication pattern is to keep the client interfaces from needing to know the details surrounding these services. In fact, all you need to know is the service's token name so you can request a connection to it through the service broker.
The service broker loads all of the services that the application knows of from a XML file that describe how to connect to the services, as well as the token name that you will use to reference the service. When the interface requests the customer service request, it passes the customer request token to the service broker. Upon receiving the request, the service broker finds the definition for the token and generates the necessary information for connecting, abstracting the details of how to process it from the rest of the interface.
A client structured in this way offers a lot of flexibility. For example, you can enhance the service broker so that it can detect disconnected status for the server and route requests to local services without the user interface component having to know the application state, allowing you to create occasionally connected applications.